package com.srcptit.trainning.b2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class FileDemo {

	/**
	 * Copy file from f1 to f2
	 * @param f1 origin file
	 * @param f2 destination file
	 * Example: copy file from "D:\music\kiepdoden.mp3" to "D:\nhactre\kiepdodenDuyManh.mp3"
	 */
	public static void copyFile(String f1, String f2) {
		// Read data from f1 use FileInputStream
		FileInputStream fis = null;
		// Write data into f2 use FileOutputStream
		FileOutputStream fos = null;
		int data;
		try {
			fis = new FileInputStream(f1);
			fos = new FileOutputStream(f2);
			System.out.println("Copying....");
			data = fis.read();	// Read data
			while(data != -1) {
				fos.write(data);	// Write data
				data = fis.read();	// Read data util end of file
			}
			System.out.println("Done!");
			// Close file Stream
			fis.close();
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("File: " + f1 + " or "+ f2 + " is not found!");
		} catch (IOException e) {
			System.out.println("Error: " + e.getMessage());
		}
		
	}
	
	public static void main(String[] args) {
		String f1;
		String f2;
		System.out.println("-- COPPY FILE --");
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter origin file: ");
		f1 = sc.nextLine();
		System.out.println("Enter destination file: ");
		f2 = sc.nextLine();
		copyFile(f1, f2);
	}
	
}
